package org.example.cotroller.goods;


import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.example.entity.Goods;
import org.example.entity.PageBean;
import org.example.entity.Result;
import org.example.service.GoodsService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/goods")
public class GoodsController {

    @Resource
    private GoodsService goodsService;
    /**
     * 处理商品请求
     **/

    /**
     *新增商品请求
     **/

//    @PostMapping("")
//    public Result addGoods(@RequestBody String json) {
//        log.info("新增商品:{}",json);
//
//        return Result.success();
//    }
    @PostMapping("")
    public Result addGoods(@RequestBody Goods goods) {
        log.info("新增商品:{}",goods);
        goodsService.addGoods(goods);
        return Result.success();
    }

    /**
     *处理获取商品列表请求请求
     **/

    @GetMapping("")
    public Result<PageBean<Goods>> getGoods(@RequestParam(value = "goodsName",required = false)String goodsName,
                                            @RequestParam(value = "categoryName",required = false)String categoryName,
                                            @RequestParam(value = "status",required = false)String status,
                                            @RequestParam(value = "pageSize",required = false, defaultValue = "10") Integer pageSize,
                                  @RequestParam(value = "page",required = false, defaultValue = "1")Integer page){
        log.info("获取列表:");
        PageBean<Goods> goodsPageBean = goodsService.listByReason(pageSize,page,goodsName,categoryName,status);
        return Result.success(goodsPageBean);
    }

    /**
     * 处理删除用户请求
     * */

    @GetMapping("/{goodsId}")
    public Result deleteGoods(@PathVariable String goodsId){
        log.info("删除：{}",goodsId);
        goodsService.delete(Integer.parseInt(goodsId));
        return Result.success();

    }
    /**
     * 处理更新菜品请求
     **/

    @PostMapping("/{goodsId}")
    public Result updateGoods(@RequestBody Goods goods,@PathVariable Integer goodsId){
        log.info("更新：{}",goods.getGoodsId());
        goodsService.update(goods);

        return Result.success();


    }
    @GetMapping("/list")
    public Result<List<String>> getGoodsList(){
        return Result.success(goodsService.list());
    }
}

